home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SATAN11.ZIP / SRC / RPCGEN / RPC_COUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-09  |  8.0 KB  |  375 lines

  1. /* @(#)rpc_cout.c    2.1 88/08/01 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #ifndef lint
  31. static char sccsid[] = "@(#)rpc_cout.c 1.8 87/06/24 (C) 1987 SMI";
  32. #endif
  33.  
  34. /*
  35.  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler 
  36.  * Copyright (C) 1987, Sun Microsystems, Inc. 
  37.  */
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include "rpc_util.h"
  43. #include "rpc_parse.h"
  44.  
  45. static print_header();
  46. static print_trailer();
  47. static space();
  48. static emit_enum();
  49. static emit_union();
  50. static emit_struct();
  51. static emit_typedef();
  52. static print_stat();
  53.  
  54. /*
  55.  * Emit the C-routine for the given definition 
  56.  */
  57. void
  58. emit(def)
  59.     definition *def;
  60. {
  61.     if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
  62.         return;
  63.     }
  64.     print_header(def);
  65.     switch (def->def_kind) {
  66.     case DEF_UNION:
  67.         emit_union(def);
  68.         break;
  69.     case DEF_ENUM:
  70.         emit_enum(def);
  71.         break;
  72.     case DEF_STRUCT:
  73.         emit_struct(def);
  74.         break;
  75.     case DEF_TYPEDEF:
  76.         emit_typedef(def);
  77.         break;
  78.     }
  79.     print_trailer();
  80. }
  81.  
  82. static
  83. findtype(def, type)
  84.     definition *def;
  85.     char *type;
  86. {
  87.     if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
  88.         return (0);
  89.     } else {
  90.         return (streq(def->def_name, type));
  91.     }
  92. }
  93.  
  94. static
  95. undefined(type)
  96.     char *type;
  97. {
  98.     definition *def;
  99.  
  100.     def = (definition *) FINDVAL(defined, type, findtype);
  101.     return (def == NULL);
  102. }
  103.  
  104.  
  105. static
  106. print_header(def)
  107.     definition *def;
  108. {
  109.     space();
  110.     f_print(fout, "bool_t\n");
  111.     f_print(fout, "xdr_%s(xdrs, objp)\n", def->def_name);
  112.     f_print(fout, "\tXDR *xdrs;\n");
  113.     f_print(fout, "\t%s ", def->def_name);
  114.     if (def->def_kind != DEF_TYPEDEF ||
  115.         !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
  116.         f_print(fout, "*");
  117.     }
  118.     f_print(fout, "objp;\n");
  119.     f_print(fout, "{\n");
  120. }
  121.  
  122. static
  123. print_trailer()
  124. {
  125.     f_print(fout, "\treturn (TRUE);\n");
  126.     f_print(fout, "}\n");
  127.     space();
  128. }
  129.  
  130.  
  131. static
  132. print_ifopen(indent, name)
  133.     int indent;
  134.     char *name;
  135. {
  136.     tabify(fout, indent);
  137.     f_print(fout, "if (!xdr_%s(xdrs", name);
  138. }
  139.  
  140.  
  141. static
  142. print_ifarg(arg)
  143.     char *arg;
  144. {
  145.     f_print(fout, ", %s", arg);
  146. }
  147.  
  148.  
  149. static
  150. print_ifsizeof(prefix, type)
  151.     char *prefix;
  152.     char *type;
  153. {
  154.     if (streq(type, "bool")) {
  155.         f_print(fout, ", sizeof(bool_t), xdr_bool");
  156.     } else {
  157.         f_print(fout, ", sizeof(");
  158.         if (undefined(type) && prefix) {
  159.             f_print(fout, "%s ", prefix);
  160.         }
  161.         f_print(fout, "%s), xdr_%s", type, type);
  162.     }
  163. }
  164.  
  165. static
  166. print_ifclose(indent)
  167.     int indent;
  168. {
  169.     f_print(fout, ")) {\n");
  170.     tabify(fout, indent);
  171.     f_print(fout, "\treturn (FALSE);\n");
  172.     tabify(fout, indent);
  173.     f_print(fout, "}\n");
  174. }
  175.  
  176. static
  177. space()
  178. {
  179.     f_print(fout, "\n\n");
  180. }
  181.  
  182. static
  183. print_ifstat(indent, prefix, type, rel, amax, objname, name)
  184.     int indent;
  185.     char *prefix;
  186.     char *type;
  187.     relation rel;
  188.     char *amax;
  189.     char *objname;
  190.     char *name;
  191. {
  192.     char *alt = NULL;
  193.  
  194.     switch (rel) {
  195.     case REL_POINTER:
  196.         print_ifopen(indent, "pointer");
  197.         print_ifarg("(char **)");
  198.         f_print(fout, "%s", objname);
  199.         print_ifsizeof(prefix, type);
  200.         break;
  201.     case REL_VECTOR:
  202.         if (streq(type, "string")) {
  203.             alt = "string";
  204.         } else if (streq(type, "opaque")) {
  205.             alt = "opaque";
  206.         }
  207.         if (alt) {
  208.             print_ifopen(indent, alt);
  209.             print_ifarg(objname);
  210.         } else {
  211.             print_ifopen(indent, "vector");
  212.             print_ifarg("(char *)");
  213.             f_print(fout, "%s", objname);
  214.         }
  215.         print_ifarg(amax);
  216.         if (!alt) {
  217.             print_ifsizeof(prefix, type);
  218.         }
  219.         break;
  220.     case REL_ARRAY:
  221.         if (streq(type, "string")) {
  222.             alt = "string";
  223.         } else if (streq(type, "opaque")) {
  224.             alt = "bytes";
  225.         }
  226.         if (streq(type, "string")) {
  227.             print_ifopen(indent, alt);
  228.             print_ifarg(objname);
  229.         } else {
  230.             if (alt) {
  231.                 print_ifopen(indent, alt);
  232.             } else {
  233.                 print_ifopen(indent, "array");
  234.             }
  235.             print_ifarg("(char **)");
  236.             if (*objname == '&') {
  237.                 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
  238.                     objname, name, objname, name);
  239.             } else {
  240.                 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
  241.                     objname, name, objname, name);
  242.             }
  243.         }
  244.         print_ifarg(amax);
  245.         if (!alt) {
  246.             print_ifsizeof(prefix, type);
  247.         }
  248.         break;
  249.     case REL_ALIAS:
  250.         print_ifopen(indent, type);
  251.         print_ifarg(objname);
  252.         break;
  253.     }
  254.     print_ifclose(indent);
  255. }
  256.  
  257.  
  258. /* ARGSUSED */
  259. static
  260. emit_enum(def)
  261.     definition *def;
  262. {
  263.     print_ifopen(1, "enum");
  264.     print_ifarg("(enum_t *)objp");
  265.     print_ifclose(1);
  266. }
  267.  
  268.  
  269. static
  270. emit_union(def)
  271.     definition *def;
  272. {
  273.     declaration *dflt;
  274.     case_list *cl;
  275.     declaration *cs;
  276.     char *object;
  277.     char *vectorfmt = "objp->%s_u.%s";
  278.     char *format = "&objp->%s_u.%s";
  279.  
  280.     print_stat(&def->def.un.enum_decl);
  281.     f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
  282.     for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
  283.         cs = &cl->case_decl;
  284.         f_print(fout, "\tcase %s:\n", cl->case_name);
  285.         if (!streq(cs->type, "void")) {
  286.             object = alloc(strlen(def->def_name) + strlen(format) +
  287.                        strlen(cs->name) + 1);
  288.             if (isvectordef(cs->type, cs->rel)) {
  289.                 s_print(object, vectorfmt, def->def_name, 
  290.                     cs->name);
  291.             } else {
  292.                 s_print(object, format, def->def_name, 
  293.                     cs->name);
  294.             }
  295.             print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
  296.                      object, cs->name);
  297.             free(object);
  298.         }
  299.         f_print(fout, "\t\tbreak;\n");
  300.     }
  301.     dflt = def->def.un.default_decl;
  302.     if (dflt != NULL) {
  303.         if (!streq(dflt->type, "void")) {
  304.             f_print(fout, "\tdefault:\n");
  305.             object = alloc(strlen(def->def_name) + strlen(format) +
  306.                        strlen(dflt->name) + 1);
  307.             if (isvectordef(dflt->type, dflt->rel)) {
  308.                 s_print(object, vectorfmt, def->def_name, 
  309.                     dflt->name);
  310.             } else {
  311.                 s_print(object, format, def->def_name, 
  312.                     dflt->name);
  313.             }
  314.             print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
  315.                      dflt->array_max, object, dflt->name);
  316.             free(object);
  317.             f_print(fout, "\t\tbreak;\n");
  318.         }
  319.     } else {
  320.         f_print(fout, "\tdefault:\n");
  321.         f_print(fout, "\t\treturn (FALSE);\n");
  322.     }
  323.     f_print(fout, "\t}\n");
  324. }
  325.  
  326.  
  327.  
  328. static
  329. emit_struct(def)
  330.     definition *def;
  331. {
  332.     decl_list *dl;
  333.  
  334.     for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
  335.         print_stat(&dl->decl);
  336.     }
  337. }
  338.  
  339.  
  340.  
  341.  
  342. static
  343. emit_typedef(def)
  344.     definition *def;
  345. {
  346.     char *prefix = def->def.ty.old_prefix;
  347.     char *type = def->def.ty.old_type;
  348.     char *amax = def->def.ty.array_max;
  349.     relation rel = def->def.ty.rel;
  350.  
  351.     print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
  352. }
  353.  
  354.  
  355.  
  356.  
  357.  
  358. static
  359. print_stat(dec)
  360.     declaration *dec;
  361. {
  362.     char *prefix = dec->prefix;
  363.     char *type = dec->type;
  364.     char *amax = dec->array_max;
  365.     relation rel = dec->rel;
  366.     char name[256];
  367.  
  368.     if (isvectordef(type, rel)) {
  369.         s_print(name, "objp->%s", dec->name);
  370.     } else {
  371.         s_print(name, "&objp->%s", dec->name);
  372.     }
  373.     print_ifstat(1, prefix, type, rel, amax, name, dec->name);
  374. }
  375.